package com.skit.schedule.fragment

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.skit.common.ResourcesManager
import com.skit.common.bean.ActivityBean
import com.skit.schedule.R
import kotlinx.android.synthetic.main.schedule_item_activity_layout.view.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author shuike <a href="mailto:shuike007@gmail.com">Contact me.</a>
 * @version 1.0
 * @since  2020/6/17
 */
class ScheduleRecyclerAdapter(private val list: MutableList<ActivityBean>) :
    RecyclerView.Adapter<ScheduleRecyclerAdapter.ViewHolder>() {
    private val format = SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault())

    var onItemClickListener: ((View, Int) -> Unit?)? = null

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.image
        val title: TextView = itemView.title
        val activity_start_time: TextView = itemView.activity_start_time
        val apply_time: TextView = itemView.apply_time
        val event_location: TextView = itemView.event_location

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflate =
            LayoutInflater.from(parent.context)
                .inflate(R.layout.schedule_item_activity_layout, parent, false)
        return ViewHolder(
            inflate
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val bean = list[position];

        Glide.with(holder.itemView).load(bean.activityImages[0])
            .into(holder.imageView)
        holder.title.text = bean.activityName
        holder.event_location.text = ResourcesManager.getRes().getString(
            R.string.main_event_location, bean.eventLocation
        )

        holder.activity_start_time.text = ResourcesManager.getRes().getString(
            R.string.main_activity_time,
            format.format(bean.startTime),
            format.format(bean.endTime)
        )

        holder.apply_time.text = ResourcesManager.getRes().getString(
            R.string.main_apply_time,
            format.format(bean.signUpStartTime),
            format.format(bean.signUpEndTime)
        )

        holder.itemView.setOnClickListener {
            onItemClickListener?.invoke(it, position)
        }

        holder.itemView.number_of_apply.text = ResourcesManager.getRes().getString(
            R.string.main_xiegang, bean.registeredNum, bean.quotaNum
        )
    }
    override fun getItemCount(): Int = list.size
}